﻿using JESAI.Application.Abstractions.Sys;
using JESAI.Application.Abstractions.Sys.Dtos;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.AutoWebApi.Setting;
using JESAI.Core.Attributes;
using JESAI.DDD.Services;
using JESAI.Platform.Domain.Admin.Entities;
using JESAI.Platform.Domain.Admin.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Sys
{
    [AppService]
    public class LoginService : ApplicationService,ILoginService, IAutoApiService
    {
        private readonly IUserRepository _userRepository;
        public LoginService(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public UserInfoDto GetUserInfoByUserName(string userName)
        {
            var userList = _userRepository._DbQueryable.Where(p => p.UserName == userName).ToList();

            return userList.FirstOrDefault().MapTo<UserInfoDto>();
        }

        public List<RoleDto> GetUserRoleByUserId(long userId)
        {
          return  _userRepository._DbQueryable.Context.Queryable<RoleModel, UserRoleModel>((role, urole) => new object[] { JoinType.Inner, role.Id == urole.RoleId })
              .Where((role, urole) => urole.Uid == userId)
              .Where((role, urole) => role.IsDel == false)
              .Select((role, urole) => new { role.Key })
              .ToList().MapToList<RoleDto>();
        }
    }
}
